package com.se.domain.mysql;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import com.se.domain.request.param.CreateActParam;
import com.se.domain.request.param.UpdateActParam;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
public class ActivityDO {
    Long actId;
    Long userId;
    String catIds;
    String title;
    String info;
    String place;

    String startTime;
    String endTime;
    String applyTime;
    String applyEndTime;
    Integer size;
    Integer applyNum;
    Boolean publish;

    Long qnId;
    String qnJson;

    String imgIds;

    public List<Long> getImgIdList(){
        if(StrUtil.isBlank(this.imgIds)){
            return Collections.emptyList();
        }else return Arrays.stream(this.imgIds.split(",")).map(Long::parseLong).toList();
    }

    Boolean active;
    Date ctime;
    Date utime;

    public ActivityDO(CreateActParam param, Long userId) {
        this.userId = userId;

        this.catIds = param.getCatIds()
                .stream()
                .map(Object::toString)
                .collect(Collectors.joining(","));
        this.title = param.getTitle();
        this.info = param.getInfo();
        this.place = param.getPlace();
        this.startTime = param.getStartTime();
        this.endTime = param.getEndTime();
        this.applyTime = param.getApplyTime();
        this.applyEndTime = param.getApplyEndTime();
        this.size = param.getSize();
        this.qnJson = new JSONArray(param.getQn()).toString();
        this.imgIds = param.getImgIds();
    }

    public void update(UpdateActParam param) {
        this.title = param.getTitle();
        this.info = param.getInfo();
        this.place = param.getPlace();
        this.startTime = param.getStartTime();
        this.endTime = param.getEndTime();
        this.applyTime = param.getApplyTime();
        this.applyEndTime = param.getApplyEndTime();
        this.size = param.getSize();
        this.qnJson = new JSONArray(param.getQn()).toString();
        this.imgIds = param.getImgIds();
    }

    public String sizeNumStr() {
        if(this.size > 0){
            return this.applyNum.toString() + "/" + this.size;
        }else {
            return this.applyNum.toString();
        }
    }
}
